package org.zero.onlinesubject.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.zero.onlinesubject.common.vo.ClassInfoVo;
import org.zero.onlinesubject.common.vo.Result;
import org.zero.onlinesubject.common.vo.UserVo;
import org.zero.onlinesubject.entity.ClassInfo;
import org.zero.onlinesubject.entity.User;

import org.zero.onlinesubject.service.ClassInfoService;
import org.zero.onlinesubject.service.UserService;
import org.zero.onlinesubject.utils.IDWorker;
import org.zero.onlinesubject.utils.UserContextHolder;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/class/api")
public class ClassController {

    @Resource
    private ClassInfoService classInfoService;

    @Resource
    private UserService userService;

    @GetMapping("/getBizId")
    public Result<String> createBizId(){
        return Result.ok(IDWorker.createCLSID());
    }

    // 获取班级信息
    @GetMapping("/clsInfo")
    public Result<ClassInfoVo> getClsInfo(String classId){
        ClassInfo classInfo = classInfoService.getById(classId);
        if(classInfo==null){
            return Result.error(200,"班级信息不存在");
        }
        UserVo userVo = userService.getUserVo(classInfo.getHeadteacherid());
        if(userVo==null){
            return Result.error(500,"当前班级信息中缺少导员信息");
        }
        ClassInfoVo classInfoVo = new ClassInfoVo();
        BeanUtils.copyProperties(classInfo,classInfoVo);
        classInfoVo.setUserVo(userVo);

        return Result.ok(classInfoVo);
    }

    // 学生加入班级
    @GetMapping("/joinClass/{classId}/{userId}")
    public Result<String> joinClass(@PathVariable("classId") String classId, @PathVariable("userId") String userId){
        ClassInfo classInfo = classInfoService.getById(classId);
        if(classInfo==null){
            return Result.error(200,"所加入的班级不存在,请检查后重试");
        }
        Boolean flag = classInfoService.joinClass(classId,userId);
        return flag ? Result.ok("加入班级成功") : Result.error(500,"加入班级失败，请检查用户是否已加入班级");
    }

    // 创建班级
    @PostMapping("/addClass")
    public Result<String> addClass(@RequestBody ClassInfo classInfo){
        Boolean flag = classInfoService.createClass(classInfo);
        if(!flag){
            return Result.error(500,"创建班级失败");
        }
        return Result.ok("创建班级成功");
    }

    // 解散班级
    @PostMapping("/delClass")
    public Result<String> delClass(String classId){
        if(StringUtils.isNullOrEmpty(classId)){
            return Result.error(500,"班级ID不能为空");
        }
        ClassInfo classInfo = classInfoService.getById(classId);
        User user = UserContextHolder.get();
        if(!user.getUserid().equals(classInfo.getHeadteacherid())){
            return Result.error(500,"您没有删除该班级的权限");
        }
        boolean b = classInfoService.removeCls(classId);

        if(!b){
            return Result.error(500,"删除课程信息失败");
        }
        return Result.ok("删除课程成功");
    }

    // 获取当前教师对应的班级列表
    @GetMapping("/clsList")
    public Result<List<ClassInfo>> getClassList(){
        User user = UserContextHolder.get();
        String userId = user.getUserid();
        return  Result.ok(classInfoService.getClassList(userId));
    }

    // 将教师分配到对应的班级
    @GetMapping("/asignTeacherToClass")
    public Result<String> asignTeacherToClass(String userId,String[] classList){
        Integer roleId = userService.getById(userId).getRoleid();
        if(roleId!=3){
            return Result.error(500,"操作对象错误，只能将教师分配到对应的班级");
        }

        boolean flag = classInfoService.asignTeacherToClass(userId,classList);
        if(!flag){
            return Result.error(500,"分配教师到班级失败");
        }
        return Result.ok("分配成功");
    }


    // 获取总的课程列表
    @GetMapping("/allClassList")
    public Result<List<ClassInfo>> getAllClassList(String classname,Integer current,Integer size){
        if(size == null || size == 0){
            size = 10;
        }
        if(current == null){
            current = 0;
        }else{
            current = (current - 1) * size;
        }

        List<ClassInfo> classInfos = classInfoService.getClassByCond(classname,current,size);
        return Result.ok(classInfos);
    }

    // 获取用户加入的班级
    @GetMapping("/getJoinClass")
    public Result<List<ClassInfo>> getJoinClass(){
        User user = UserContextHolder.get();
        List<ClassInfo> list = classInfoService.getJoinClass(user.getUserid());
        return Result.ok(list);
    }
}

